package fun.formoji.commons.util;

import java.util.*;

public class ListUtils {

    public static <T> List<List<T>> splitList(List<T> list, int len) {

        if (list == null || list.isEmpty() || len < 1) {
            return Collections.emptyList();
        }

        List<List<T>> result = new ArrayList<>();

        int size = list.size();
        int count = (size + len - 1) / len;

        for (int i = 0; i < count; i++) {
            List<T> subList = list.subList(i * len, (Math.min((i + 1) * len, size)));
            result.add(subList);
        }

        return result;
    }

    public static <T> List<T> randomList(List<T> list, int size) {
        size = Math.min(list.size(), size);
        int count = 0;
        Map<Integer, String> map = new HashMap<>();
        List<T> newList = new ArrayList<>();
        if (list.size() == 0) {
            return list;
        } else {
            while (count < size) {
                int random = (int) (Math.random() * list.size());
                if (!map.containsKey(random)) {
                    map.put(random, "");
                    newList.add(list.get(random));
                    count++;
                }
            }
            return newList;
        }
    }

    public static <T> List<T> removeSubList(List<T> list, List<T> sublist) {
        list.removeIf(sublist::contains);
        return list;
    }

    public static boolean isEmpty(List list){
        return list == null || list.isEmpty();
    }
}

